 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.utils
 * File     : V.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 11, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.utils;

import java.util.Date;

import net.solosky.easyavatar.api.ErrorCode;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

/**
 *
 * 界面工具类
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class Views {
	
	public static Context ctx;
	
	
	public static void setContext(Context context){
		ctx = context;
	}
	/**
	 * 获取res字符串资源
	 * @param ctx
	 * @param str
	 * @return
	 */
	public static String S(String str){		
		if(ctx==null)
			return str;
		int i = ctx.getResources().getIdentifier(str,"string", ctx.getPackageName());
		return ctx.getResources().getString(i);
	}
	
	public static String S(int stringId){
		return ctx.getResources().getString(stringId);
	}
	
	/**
	 * 根据错误代码返回错误字符串
	 * @param code
	 * @return
	 */
	public static String E(ErrorCode code){
		return S("err_"+code.toString().toLowerCase());
	}

	
	/**
	 * 快速创建一个message对象
	 * @param arg1
	 * @param arg2
	 * @param obj
	 * @return
	 */
	public static Message M(int arg1, int arg2, Object obj){
		Message message = new Message();
		message.arg1 = arg1;
		message.arg2 = arg2;
		message.obj = obj;
		return message;
	}
	
	/**
	 * 显示使用alertDialog提示信息
	 * @param title			提示标题
	 * @param message		提示内容
	 * @param context		父窗口对象
	 */
	public static AlertDialog.Builder showMessage(String title, String message, final DialogInterface.OnClickListener onClickListener ){
		final AlertDialog.Builder builder=new AlertDialog.Builder(ctx);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(onClickListener!=null)
					onClickListener.onClick(dialog, which);
				AlertDialog ad=builder.create();
				ad.cancel();
			}
		});
		builder.show();
		return builder;
	}
	
	/**
	 * 显示使用alertDialog确认信息，需要用户点击确认或者取消
	 * @param title			提示标题
	 * @param message		提示内容
	 * @param context		父窗口对象
	 */
	public static AlertDialog.Builder showConfirm(String title, String message,
			final DialogInterface.OnClickListener onOkListener, final DialogInterface.OnClickListener onCancelListener){
		final AlertDialog.Builder builder=new AlertDialog.Builder(ctx);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(onOkListener!=null){
					onOkListener.onClick(dialog, which);
				}
				AlertDialog ad=builder.create();
				ad.cancel();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(onCancelListener!=null){
					onCancelListener.onClick(dialog, which);
				}
				AlertDialog ad=builder.create();
				ad.cancel();
			}
		});
		
		builder.show();
		return builder;
	}
	
	
	/**
	 * 快速显示一个toast
	 * @param message		消息内容
	 * @param duration		显示时间
	 * @param context		父窗口对象
	 * @return
	 */
	public static void showToast(String message, int duration){
		Toast.makeText(ctx, message, duration).show();
	}
	
	public static void showToast(int stringId){
		Toast.makeText(ctx, ctx.getResources().getString(stringId),(int) Constants.TOAST_DISPLAY_TIME).show();
	}
	
	public static void showToast(String message){
		Toast.makeText(ctx, message, (int) Constants.TOAST_DISPLAY_TIME).show();
	}
	
	/**
	 * 批量添加点击事件
	 * @param ids
	 * @param listener
	 * @param activity
	 */
	public static void batchSetClickListener(int[] ids, View.OnClickListener listener, Activity activity){
		for(int i=0; i<ids.length; i++){
			View view = activity.findViewById(ids[i]);
			view.setOnClickListener(listener);
		}
	}
	
	//传入一个Date，判断是多久前，返回示例"4小时前"
	public static String formatGoodTime(Date date){
		String rs = "刚刚";
		Long i = date.getTime();
		Date now = new Date();
		Long j = now.getTime();
		long day = 1000 * 60 * 60 * 24;
		long hour = 1000 * 60 * 60;
		long min = 1000 * 60;
		long sec = 1000;
		if (((j-i)/day)>0)
			rs = ((j-i)/day)+"天前";
		else if (((j-i)/hour)>0)
			rs = ((j-i)/hour)+"小时前";
		else if (((j-i)/min)>0)
			rs = ((j-i)/min)+"分钟前";
		else if (((j-i)/sec)>0)
			rs = ((j-i)/sec)+"秒前";
		return rs;
	}
	
}
